package com.guodu.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.MaterialAppraisalDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.ViewMaterialAppraisalDao;
import com.guodu.dao.ViewMaterialAppraisalThreeDao;
import com.guodu.entity.MaterialAppraisal;
import com.guodu.entity.Units;
import com.guodu.entity.ViewMaterialAppraisal;
import com.guodu.entity.ViewMaterialAppraisalCom;
import com.guodu.entity.ViewMaterialAppraisalThree;
import com.guodu.entity.ViewMaterialAppraisalYear;
import com.guodu.service.MaterialAppraisalService;
import com.guodu.vo.MaterialAppraisalQueryVo;
import com.guodu.vo.MaterialAppraisalVo;


@Service("materialAppraisalService")
public class MaterialAppraisalServiceImpl implements MaterialAppraisalService {

	
	@Resource
	private MaterialAppraisalDao materialAppraisalDao;
	
	@Resource
	private ViewMaterialAppraisalDao viewMaterialAppraisalDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private ViewMaterialAppraisalThreeDao viewMaterialAppraisalThreeDao;
	
	
	@Override
	public Map<String,MaterialAppraisalVo> findByVo(MaterialAppraisalQueryVo maqv,
			Units units) {
		// TODO Auto-generated method stub
		Map<String,MaterialAppraisalVo> map = new HashMap<String,MaterialAppraisalVo>();
		List<MaterialAppraisal> list = materialAppraisalDao.findByVo(maqv,units);
		List<ViewMaterialAppraisalYear> viewsYear=viewMaterialAppraisalDao.findByYear(maqv.getMonths());
		List<ViewMaterialAppraisal> views = viewMaterialAppraisalDao.findByMonth(maqv.getMonths());
		List<Object[]> ljList = materialAppraisalDao.findLjCount(maqv);
		List<Units> unitses =unitsDao.findAllFunits();
		for(Units u :unitses){
			MaterialAppraisalVo vo = new MaterialAppraisalVo();
			vo.setCount(unitsDao.findUnitsByFid(u.getUnitsId()).size());
			map.put(u.getUnitsId()+"Count", vo);
		}
		for(int i=0;i<list.size();i++){
			MaterialAppraisalVo materialAppraisalVo = new MaterialAppraisalVo(list.get(i));
			materialAppraisalVo.setPaiming(i+1L);
			map.put(list.get(i).getUnitsId().toString()+"app", materialAppraisalVo);
		}
		for(int i=0;i<ljList.size();i++){
			if(map.get(ljList.get(i)[0]+"app")!=null){
				map.get(ljList.get(i)[0]+"app").setLjCount(new BigDecimal(ljList.get(i)[1].toString()));
				map.get(ljList.get(i)[0]+"app").setLjPaiming(i+1L);
			}else{
				MaterialAppraisalVo materialAppraisalVo = new MaterialAppraisalVo();
				materialAppraisalVo.setLjCount(new BigDecimal(ljList.get(i)[1].toString()));
				materialAppraisalVo.setLjPaiming(i+1L);
				map.put(ljList.get(i)[0].toString()+"app", materialAppraisalVo);
			}
		}
		for(ViewMaterialAppraisal v :views){
			if(v.getFunitsId()!=null){
			if(map.get(v.getFunitsId().toString()+"app")!=null){
				map.get(v.getFunitsId().toString()+"app").setView(v);
			}else{
				map.put(v.getFunitsId().toString()+"app", new MaterialAppraisalVo(v));
			}
			}
			
		}
		for(ViewMaterialAppraisalYear v :viewsYear){
			if(v.getFunitsId()!=null){
			if(map.get(v.getFunitsId().toString()+"app")!=null){
				if(map.get(v.getFunitsId().toString()+"app").getView()!=null){
					map.get(v.getFunitsId().toString()+"app").getView().setPurchaseBiddingPlan(v.getPurchaseBiddingPlan());
					map.get(v.getFunitsId().toString()+"app").getView().setPurchaseBiddingPlanTwo(v.getPurchaseBiddingPlanTwo());
					map.get(v.getFunitsId().toString()+"app").getView().setPurchaseDistributionPlan(v.getPurchaseDistributionPlan());
					map.get(v.getFunitsId().toString()+"app").getView().setJoinStore(v.getJoinStore());
					map.get(v.getFunitsId().toString()+"app").getView().setStore(v.getStore());
					map.get(v.getFunitsId().toString()+"app").getView().setCliqueRed(v.getCliqueRed());
					map.get(v.getFunitsId().toString()+"app").getView().setSupplierQualified(v.getSupplierQualified());
					map.get(v.getFunitsId().toString()+"app").getView().setSupplierRedTwo(v.getSupplierRedTwo());
					map.get(v.getFunitsId().toString()+"app").getView().setSupplierEvaluation(v.getSupplierEvaluation());
					map.get(v.getFunitsId().toString()+"app").getView().setSupplierBad(v.getSupplierBad());
				}else{
					ViewMaterialAppraisal view = new ViewMaterialAppraisal();
					view.setPurchaseBiddingPlan(v.getPurchaseBiddingPlan());
					view.setPurchaseBiddingPlanTwo(v.getPurchaseBiddingPlanTwo());
					view.setPurchaseDistributionPlan(v.getPurchaseDistributionPlan());
					view.setJoinStore(v.getJoinStore());
					view.setStore(v.getStore());
					view.setCliqueRed(v.getCliqueRed());
					view.setSupplierQualified(v.getSupplierQualified());
					view.setSupplierRedTwo(v.getSupplierRedTwo());
					view.setSupplierEvaluation(v.getSupplierEvaluation());
					view.setSupplierBad(v.getSupplierBad());
					map.get(v.getFunitsId().toString()+"app").setView(view);
				}
			}else{
				map.put(v.getFunitsId().toString()+"app", new MaterialAppraisalVo(v));
			}
			}
			
		}
		return map;
	}


	@Override
	public MaterialAppraisal findById(String purchaseId) {
		// TODO Auto-generated method stub
		return materialAppraisalDao.findById(purchaseId);
	}


	@Override
	public boolean saveorUpdate(MaterialAppraisal entity) {
		// TODO Auto-generated method stub
		if(entity.getAppraisalId()==null||entity.getAppraisalId().equals("")){
			entity.setAppraisalId("1");
		}
		materialAppraisalDao.saveorUpdate(entity);
		return true;
	}


	@Override
	public Map<String, MaterialAppraisalVo> findByVo1(
			MaterialAppraisalQueryVo maqv, Units units) {
		// TODO Auto-generated method stub
		Map<String,MaterialAppraisalVo> map = new HashMap<String,MaterialAppraisalVo>();
		if(units.getIsComplete()==0L){
			List<ViewMaterialAppraisalThree> views = viewMaterialAppraisalThreeDao.findByMonth(maqv.getMonths(),units.getUnitsId());
			for(ViewMaterialAppraisalThree v :views){
				if(v.getUnitsId()!=null){
				if(map.get(v.getUnitsId().toString()+"app")!=null){
					map.get(v.getUnitsId().toString()+"app").setView1(v);
				}else{
					map.put(v.getUnitsId().toString()+"app", new MaterialAppraisalVo(v));
				}
				}
				
			}
		}else{
			List<ViewMaterialAppraisalCom> views = viewMaterialAppraisalThreeDao.findComByMonth(maqv.getMonths(),units.getUnitsId());
			for(ViewMaterialAppraisalCom v :views){
				if(v.getUnitsId()!=null){
				if(map.get(v.getUnitsId().toString()+"app")!=null){
					map.get(v.getUnitsId().toString()+"app").setView2(v);
				}else{
					map.put(v.getUnitsId().toString()+"app", new MaterialAppraisalVo(v));
				}
				}
			}
		}
		return map;
	}

}
